﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IOEntity;
using System.Data;
using System.Data.SqlClient;

namespace IODataAccess
{
    public class clsDANews
    {
        clsDAConnection con = new clsDAConnection();
        public clsNews GetById(int id)
        {
            try
            {
                clsNews obj = new clsNews();
                obj.CateNew = new clsCateNew();
                string str = "select * from tblnews where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (String.IsNullOrEmpty(dr["hinhanh"].ToString()))
                    {
                        obj.HinhAnh = "~/Styles/Images/no-photo.gif";
                    }
                    else
                    {
                        obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString();
                    }


                    if (dr["hot"] != DBNull.Value)
                    {
                        obj.Hot = Convert.ToBoolean(dr["hot"].ToString());
                    }
                    if (dr["ngaydang"] != DBNull.Value)
                    {
                        obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString());
                    }
                    if (dr["loaitinid"] != DBNull.Value)
                    {
                        obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString());
                        obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString());
                    }
                    if (dr["TypeID"] != DBNull.Value)
                    {
                        obj.TypeID = Convert.ToInt32(dr["TypeID"].ToString());
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsNews> GetAll()
        {
            try
            {
                List<clsNews> list = new List<clsNews>();
                string str = "select * from tblnews where typeid is null order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsNews obj = new clsNews();
                    obj.CateNew = new clsCateNew();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (String.IsNullOrEmpty(dr["hinhanh"].ToString()))
                    {
                        obj.HinhAnh = "~/Styles/Images/no-photo.gif";
                    }
                    else
                    {
                        obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString();
                    }
                    if (dr["hot"] != DBNull.Value)
                    {
                        obj.Hot = Convert.ToBoolean(dr["hot"].ToString());
                    }
                    if (dr["ngaydang"] != DBNull.Value)
                    {
                        obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString());
                    }
                    if (dr["loaitinid"] != DBNull.Value)
                    {
                        obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString());
                        obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsNews> GetByHaLong()
        {
            try
            {
                List<clsNews> list = new List<clsNews>();
                string str = "select * from tblnews where typeid =5 or typeid=6 order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsNews obj = new clsNews();
                    obj.CateNew = new clsCateNew();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (String.IsNullOrEmpty(dr["hinhanh"].ToString()))
                    {
                        obj.HinhAnh = "~/Styles/Images/no-photo.gif";
                    }
                    else
                    {
                        obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString();
                    }
                    if (dr["hot"] != DBNull.Value)
                    {
                        obj.Hot = Convert.ToBoolean(dr["hot"].ToString());
                    }
                    if (dr["ngaydang"] != DBNull.Value)
                    {
                        obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString());
                    }
                    if (dr["loaitinid"] != DBNull.Value)
                    {
                        obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString());
                        obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsNews> GetByLoai(int loaiid)
        {
            try
            {
                List<clsNews> list = new List<clsNews>();
                string str = "select * from tblnews where loaitinid=@loaitinid and typeid is null order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@loaitinid", loaiid);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsNews obj = new clsNews();
                    obj.CateNew = new clsCateNew();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (String.IsNullOrEmpty(dr["hinhanh"].ToString()))
                    {
                        obj.HinhAnh = "~/Styles/Images/no-photo.gif";
                    }
                    else
                    {
                        obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString();
                    }
                    if (dr["hot"] != DBNull.Value)
                    {
                        obj.Hot = Convert.ToBoolean(dr["hot"].ToString());
                    }
                    if (dr["ngaydang"] != DBNull.Value)
                    {
                        obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString());
                    }
                    if (dr["loaitinid"] != DBNull.Value)
                    {
                        obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString());
                        obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsNews> GetByType(int typeid)
        {
            try
            {
                List<clsNews> list = new List<clsNews>();
                string str = "select * from tblnews where TypeId=@typeid order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@typeid", typeid);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsNews obj = new clsNews();
                    obj.CateNew = new clsCateNew();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (String.IsNullOrEmpty(dr["hinhanh"].ToString()))
                    {
                        obj.HinhAnh = "~/Styles/Images/no-photo.gif";
                    }
                    else
                    {
                        obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString();
                    }
                    if (dr["hot"] != DBNull.Value)
                    {
                        obj.Hot = Convert.ToBoolean(dr["hot"].ToString());
                    }
                    if (dr["ngaydang"] != DBNull.Value)
                    {
                        obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString());
                    }
                    if (dr["loaitinid"] != DBNull.Value)
                    {
                        obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString());
                        obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsNews> GetByTop(int top)
        {
            try
            {
                List<clsNews> list = new List<clsNews>();
                string str = "select * from tblnews where typeid is null order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                int dem = 0;
                while (dr.Read() || dem > top)
                {
                    dem++;
                    clsNews obj = new clsNews();
                    obj.CateNew = new clsCateNew();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (String.IsNullOrEmpty(dr["hinhanh"].ToString()))
                    {
                        obj.HinhAnh = "~/Styles/Images/no-photo.gif";
                    }
                    else
                    {
                        obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString();
                    }
                    if (dr["hot"] != DBNull.Value)
                    {
                        obj.Hot = Convert.ToBoolean(dr["hot"].ToString());
                    }
                    if (dr["ngaydang"] != DBNull.Value)
                    {
                        obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString());
                    }
                    if (dr["loaitinid"] != DBNull.Value)
                    {
                        obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString());
                        obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsNews obj)
        {
            try
            {
                string str = "insert into tblnews(tieude,noidung,ghichu,hinhanh,loaitinid,hot) values(@tieude,@noidung,@ghichu,@hinhanh,@loaitinid,@hot)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@ghichu", obj.GhiChu);
                com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh);
                com.Parameters.AddWithValue("@loaitinid", obj.CateNew.Id);
                com.Parameters.AddWithValue("@hot", obj.Hot);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblnews");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int InsertByType(clsNews obj)
        {
            try
            {
                string str = "insert into tblnews(tieude,noidung,ghichu,hinhanh,loaitinid,hot,TypeID) values(@tieude,@noidung,@ghichu,@hinhanh,@loaitinid,@hot,@typeid)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@ghichu", obj.GhiChu);
                com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh);
                com.Parameters.AddWithValue("@loaitinid", obj.CateNew.Id);
                com.Parameters.AddWithValue("@hot", obj.Hot);
                com.Parameters.AddWithValue("@typeid", obj.TypeID);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblnews");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int InsertOther(clsNews obj)
        {
            try
            {
                string str = "insert into tblnews(tieude,noidung,ghichu,hinhanh,loaitinid,hot,typeid) values(@tieude,@noidung,@ghichu,@hinhanh,@loaitinid,@hot,@typeid)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@ghichu", obj.GhiChu);
                com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh);
                com.Parameters.AddWithValue("@loaitinid", obj.CateNew.Id);
                com.Parameters.AddWithValue("@hot", obj.Hot);
                com.Parameters.AddWithValue("@typeid", obj.TypeID);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblnews");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsNews obj)
        {
            try
            {
                string str = "Update tblnews set tieude=@tieude,noidung=@noidung,ghichu=@ghichu,hinhanh=@hinhanh,loaitinid=@loaitinid,hot=@hot where id=@id";
                if (obj.TypeID > 0)
                    str = "Update tblnews set tieude=@tieude,noidung=@noidung,ghichu=@ghichu,hinhanh=@hinhanh,loaitinid=@loaitinid,hot=@hot,typeid=@typeid where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@ghichu", obj.GhiChu);
                com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh);
                com.Parameters.AddWithValue("@loaitinid", obj.CateNew.Id);
                com.Parameters.AddWithValue("@hot", obj.Hot);
                com.Parameters.AddWithValue("@id", obj.Id);
                if (obj.TypeID > 0)
                    com.Parameters.AddWithValue("@typeid", obj.TypeID);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblnews where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
